﻿namespace TathamOddie.OpenSearchValidator.Logic.Tasks
{
	public class RetrieveSourcePageTask : IValidationTask
	{
	    readonly string[] sourcePageMediaTypes = new []
		{
            "text/html",
            "application/xhtml+xml"
	    };

		public ValidationTaskResult Validate(ValidationServices services, ValidationState state)
		{
			var result = new ValidationTaskResult();

			var html = DownloadHelper.Download
			(
				services.WebLoader,
				result,
				"source page",
				false,
				state.PageUri,
				sourcePageMediaTypes
			);

			if (html != null)
			{
				state.PageHtml = html.Content;
				result.ChildTasks.Add(new Description.FindOpenSearchDescriptionTask());
			}

			return result;
		}
	}
}